import 'dart:io';
import 'dart:typed_data';
import 'dart:ui';

import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:gallery_saver/gallery_saver.dart';
import 'package:loxen/dialog/toast.dart';
import 'package:loxen/utils/config.dart';
import 'package:path_provider/path_provider.dart';
import 'package:qr_flutter/qr_flutter.dart';
import 'package:loxen/dialog/loading.dart';
import 'package:sharesdk_plugin/sharesdk_plugin.dart';

class PosterWidget extends StatelessWidget {
  final String poster;
  final GlobalKey globalKey = GlobalKey();

  PosterWidget({Key key, this.poster}) : super(key: key);

  void shareToWechat(BuildContext context) async {
    LoadingToast.show("转发中···", context);
    LoadingToast.dismiss();
    SSDKMap params = SSDKMap()
      ..setGeneral("", "", [poster], poster, null, null, null, null, null, null,
          SSDKContentTypes.image);
    SharesdkPlugin.share(ShareSDKPlatforms.wechatSession, params,
        (SSDKResponseState state, Map userdata, Map contentEntity,
            SSDKError error) {
      print(error);
      print(userdata);
    });
  }

  @override
  Widget build(BuildContext context) {
    // SystemChrome.setEnabledSystemUIOverlays([]);
    return Scaffold(
      backgroundColor: Theme.of(context).backgroundColor,
      body: MediaQuery.removePadding(
          context: context,
          removeTop: true,
          child: SingleChildScrollView(
            child: Column(
              children: <Widget>[
                Container(
                  margin: EdgeInsets.only(top: 100.h, right: 55.w),
                  alignment: Alignment.topRight,
                  child: Material(
                    child: InkWell(
                      onTap: () => Navigator.pop(context),
                      child: Image.asset(
                        'images/bill_close.png',
                        width: 38.w,
                        height: 38.w,
                      ),
                    ),
                  ),
                ),
                Padding(
                  padding: EdgeInsets.only(top: 40.h),
                  child: Container(
                    width: 640.w,
                    height: 1012.w,
                    child: Image.network(
                      poster,
                      fit: BoxFit.cover,
                    ),
                  ),
                ),
                Padding(
                  padding: EdgeInsets.only(top: 40.h, bottom: 40.h),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: <Widget>[
                      Container(
                        width: 68.w,
                        height: 80.h,
                        child: InkWell(
                          onTap: () {
                            //TODO 转发
                            String path = poster.split('?')[0];
                            if (path.trim() == '') {
                              LoadingToast.dismiss();
                              Toast.show('海报不存在', context);
                              return;
                            } else {
                              shareToWechat(context);
                            }
                          },
                          child: Column(
                            children: <Widget>[
                              Image.asset(
                                'images/relay.png',
                                width: 31.w,
                                height: 34.w,
                              ),
                              Padding(
                                padding: EdgeInsets.only(top: 8.h),
                                child: Text(
                                  '转发',
                                  style: TextStyle(
                                    fontSize: 20.sp,
                                    color: Color(0xFF82868E),
                                  ),
                                ),
                              )
                            ],
                          ),
                        ),
                      ),
                      Container(
                        width: 120.w,
                      ),
                      Container(
                        width: 68.w,
                        height: 80.h,
                        child: InkWell(
                          onTap: () async {
                            //TODO 保存图片
                            String path = poster.split('?')[0];
                            if (path.trim() == '') {
                              LoadingToast.dismiss();
                              Toast.show('海报不存在', context);
                              return;
                            }
                            GallerySaver.saveImage(path).then((bool success) {
                              print(success);
                              if (success) {
                                print(path);
                                Toast.show("已保存至->相册", context,
                                    duration: Toast.LENGTH_LONG);
                              }
                            });
                          },
                          child: Column(
                            children: <Widget>[
                              Image.asset(
                                'images/save.png',
                                width: 32.w,
                                height: 33.w,
                              ),
                              Padding(
                                padding: EdgeInsets.only(top: 8.h),
                                child: Text(
                                  '保存',
                                  style: TextStyle(
                                    fontSize: 20.sp,
                                    color: Color(0xFF82868E),
                                  ),
                                ),
                              )
                            ],
                          ),
                        ),
                      )
                    ],
                  ),
                )
              ],
            ),
          )),
    );
  }
}
